<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\Redis;

/**
 * 房间手动推荐池
 */
class RoomManualManagement extends Base
{
    /**
     * 缓存的key
     *
     * @var string
     */
    private $room_manual_management_ids = 'ROOM:MANUAL:MANAGEMENT:PLATFORM:';//缓存中redis的key
    private $room_manual_management_room_ids = 'ROOM:MANUAL:MANAGEMENT:ROOM:IDS:PLATFORM:';//直接存渠道的roomid add by zxiaofee

    /**
     * 表名字
     * @var string
     */
    protected $table = 'room_manual_management';

    /**
     * 指示模型是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 模型日期列的存储格式。
     *
     * @var string
     */
    protected $dateFormat = 'U';

    /**
     * 可以被批量赋值的属性。
     *
     * @var array
     */
    protected $fillable = ['id', 'platform', 'room_uid_list', 'created_at', 'updated_at'];

    /**
     * 构造函数
     *
     * @param array $attributes
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }

    /**
     * 按渠道获取房间uid
     *
     * @param string $platform
     * @return string
     */
    public function getAllRoomUIdsByPlatform(string $platform): string
    {
        $query = self::query()->select(['room_uid_list'])->whereRaw('platform="' . $platform . '"')
            ->first();
        return $query ? $query->room_uid_list : '';
    }

    /**
     * 获取缓存名字
     *
     * @param string $platform
     * @return string
     */
    private function getCacheKeyName(string $platform): string
    {
        return $this->room_manual_management_ids . $platform;
    }

    /**
     * 清除并设置缓存
     *
     * @param string $platform 渠道
     * @return bool
     */
    public function clearAndSetCache(string $platform)
    {
        $allRoomUuids = self::getAllRoomUIdsByPlatform($platform);
        $cacheKey = $this->getCacheKeyName($platform);
        Redis::del($cacheKey);//删除缓存
        if ($allRoomUuids) {
            Redis::set($cacheKey, $allRoomUuids);
            Redis::expire($cacheKey, 7 * 24 * 3600);
        }

        return true;
    }

    /**
     * 从缓存获取手动推荐房间uid
     *
     * @param string $platform 渠道
     * @return array
     */
    public function getALlRoomUIdsFromCache(string $platform): array
    {
        $cacheKey = $this->getCacheKeyName($platform);
        $cacheValue = Redis::get($cacheKey);
        if ($cacheValue) {
            return explode(',', $cacheValue);
        }

        $this->clearAndSetCache($platform);
        $cacheValue = Redis::get($cacheKey);
        return $cacheValue ? explode(',', $cacheValue) : [];
    }

       /**
     * 从缓存获取手动推荐房间uid
     * @author zxiaofee
     * @param string $platform 渠道
     * @return array
     */
    public function getAllRoomIdsByPlatefromFromCache(string $platform)
    {
        $cacheKey = $this->room_manual_management_room_ids . $platform;
        $cacheValue = Redis::get($cacheKey);
        return json_decode($cacheValue, true);
    }

     /**
     * @desc 设置手动设置缓存
     * @author zxiaofee
     * @param string $platform 渠道
     * @param array $allRoomIds
     * @return bool
     */
    public function setAllRoomIdsByPlatefromToCache(string $platform, array $allRoomIds): bool
    {
        $cacheKey = $this->room_manual_management_room_ids . $platform;
        Redis::set($cacheKey, json_encode($allRoomIds));
        Redis::expire($cacheKey, 60 * 10);
        return true;
    }
}